package com.hbr.activity;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.hbr.R;
import com.hbr.module.Flight;

public class ResultActivity extends Activity {
    private ExpandableListView elvResult;
    private List lstResult = new ArrayList();
    private ResultAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        elvResult = (ExpandableListView) findViewById(R.id.elvResult);
        String result = getIntent().getStringExtra("result");
        adapter = new ResultAdapter();
        elvResult.setAdapter(adapter);
        try {
            parseResult(result);
        } catch (XmlPullParserException e) {
        } catch (IOException e) {
        }
    }

    private void parseResult(String result) throws XmlPullParserException, IOException {
        XmlPullParser parse = Xml.newPullParser();
        parse.setInput(new StringReader(result));
        int n = parse.next();
        Flight f = new Flight();
        while (n != XmlPullParser.END_DOCUMENT) {
            String name = parse.getName();
            switch (n) {
            case XmlPullParser.START_TAG:
                if (name.equals("AirlinesTime")) {
                    f = new Flight();
                } else {
                    String text = parse.nextText();
                    if (name.equals("Company")) {
                        f.setStrCompany(text);
                    } else if (name.equals("AirlineCode")) {
                        f.setStrAirlineCode(text);
                    } else if (name.equals("StartDrome")) {
                        f.setStrStartDrome(text);
                    } else if (name.equals("ArriveDrome")) {
                        f.setStrArriveDrome(text);
                    } else if (name.equals("StartTime")) {
                        f.setStrStartTime(text);
                    } else if (name.equals("ArriveTime")) {
                        f.setStrArriveTime(text);
                    } else if (name.equals("Mode")) {
                        f.setStrMode(text);
                    } else if (name.equals("AirlineStop")) {
                        f.setStrAirlineStop(text);
                    } else if (name.equals("Week")) {
                        f.setStrWeek(text);
                    }
                }
                break;
            case XmlPullParser.END_TAG:
                if (name.equals("AirlinesTime")) {
                    lstResult.add(f);
                }
            }
            n = parse.next();
        }
        adapter.notifyDataSetChanged();
    }

    class ResultAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return lstResult.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 9;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return lstResult.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            Flight f = (Flight) lstResult.get(groupPosition);
            String str = f.getStrCompany() + "\t" + f.getStrAirlineCode();
            if (convertView == null) {
                return getGenericView(str);
            } else {
                ((TextView) convertView).setText(str);
                return convertView;
            }
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                ViewGroup parent) {
            Flight f = (Flight) lstResult.get(groupPosition);
            String str = getChildString(childPosition, f);
            if (convertView == null) {
                return getGenericView(str);
            } else {
                ((TextView) convertView).setText(str);
                return convertView;
            }
        }

        public String getChildString(int childPosition, Flight fl) {
            switch (childPosition) {
            case 0:
                return "航空公司：" + fl.getStrCompany();
            case 1:
                return "航班号：" + fl.getStrAirlineCode();
            case 2:
                return "出发机场：" + fl.getStrStartDrome();
            case 3:
                return "到达机场：" + fl.getStrArriveDrome();
            case 4:
                return "出发时间：" + fl.getStrStartTime();
            case 5:
                return "到达时间：" + fl.getStrArriveTime();
            case 6:
                return "机型：" + fl.getStrMode();
            case 7:
                return "经停：" + fl.getStrAirlineStop();
            default:
                return "飞行周期：" + fl.getStrWeek();
            }
        }

        public TextView getGenericView(String s) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
            TextView text = new TextView(ResultActivity.this);
            text.setLayoutParams(lp);
            text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            text.setPadding(36, 0, 0, 0);
            text.setText(s);
            return text;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }

    }
}
